export default function (req, res) {
    const { body } = req;
    const { filters = {}, start = 1, limit = 10, orderBy, asc } = body;
  
    const totalItems = 120; // Fixed total for consistent pagination mock
    const startIndex = (start - 1) * limit;
  
    // Define data sets and helper functions for mock generation
    const flowTypes = [1, 2]; // 1: 进账, 2: 出账
    const billTypes = [1, 2, 50, 51, 52, 53];
    const billTypeMap = {
      1: '学生充值课时奖励',
      2: '提现银行卡失败退回',
      50: '学生退费扣回奖励',
      51: '提现银行卡',
      52: '充值课时',
      53: '购买智牛币',
    };
  
    const creatorNames = ["管理员A", "操作员B", "系统C"];
    const traderNames = ["学生张三", "财务中心", "智牛平台"];
  
    // Helper to format date-time string
    const formatDate = (date) => {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      const hours = String(date.getHours()).padStart(2, '0');
      const minutes = String(date.getMinutes()).padStart(2, '0');
      const seconds = String(date.getSeconds()).padStart(2, '0');
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    };
  
    const generateMockBill = (index) => {
      const flowType = flowTypes[Math.floor(Math.random() * flowTypes.length)];
      const billType = billTypes[Math.floor(Math.random() * billTypes.length)];
      const billAmount = parseFloat((Math.random() * 500 * (flowType === 1 ? 1 : -1) + (flowType === 1 ? 50 : -50)).toFixed(2));
      const createTime = new Date(Date.now() - Math.floor(Math.random() * 180) * 24 * 60 * 60 * 1000);
  
      const details = `${billTypeMap[billType]} - 编号:${10000 + index}`;
      const trader = traderNames[Math.floor(Math.random() * traderNames.length)];
  
      return {
        flowType: flowType,
        createTime: formatDate(createTime),
        creator: creatorNames[Math.floor(Math.random() * creatorNames.length)],
        details: details,
        billAmount: billAmount,
        trader: trader,
      };
    };
  
    const mockDataList = Array.from({ length: totalItems }, (_, i) => generateMockBill(i));
  
    // Implement filtering logic
    const filteredList = mockDataList.filter(bill => {
      // Filter by flowType
      if (filters.flowType && bill.flowType !== filters.flowType) {
        return false;
      }
  
      // Filter by billType
      if (filters.billType) {
        // Find the corresponding billType for the current mock item's details
        const currentBillType = billTypes.find(key => bill.details.startsWith(billTypeMap[key]));
        if (currentBillType !== filters.billType) {
          return false;
        }
      }
  
      // Filter by creation time range
      if (filters.createTimeMin) {
        const minDate = new Date(filters.createTimeMin);
        const billDate = new Date(bill.createTime);
        if (billDate < minDate) {
          return false;
        }
      }
      if (filters.createTimeMax) {
        const maxDate = new Date(filters.createTimeMax);
        const billDate = new Date(bill.createTime);
        if (billDate > maxDate) {
          return false;
        }
      }
  
      return true;
    });
  
    // Implement sorting logic (simplified: only sort by billAmount if orderBy is set)
    let sortedList = filteredList;
    if (orderBy === 'billAmount') {
      sortedList = [...filteredList].sort((a, b) => {
        const compare = a.billAmount - b.billAmount;
        return asc ? compare : -compare;
      });
    } else if (orderBy === 'createTime') {
      sortedList = [...filteredList].sort((a, b) => {
        const dateA = new Date(a.createTime).getTime();
        const dateB = new Date(b.createTime).getTime();
        const compare = dateA - dateB;
        return asc ? compare : -compare;
      });
    }
  
    // Apply pagination to the filtered and sorted list
    const paginatedList = sortedList.slice(startIndex, startIndex + limit);
  
    // Calculate summary (Total Bill Amount in this page)
    const summary = paginatedList.reduce((acc, item) => {
      acc.billAmount = parseFloat((acc.billAmount + item.billAmount).toFixed(2));
      return acc;
    }, {
      flowType: 0, // Placeholder
      createTime: '', // Placeholder
      creator: '汇总',
      details: '当前页总计',
      billAmount: 0.00,
      trader: 'N/A',
    });
  
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          code: 0,
          message: '成功！',
          data: {
            current: start,
            size: limit,
            total: filteredList.length,
            list: paginatedList,
            summary: summary,
          },
          timestamp: Date.now(),
          requestId: 'requestId_' + Math.random().toString(36).substring(2, 15),
          signature: 'signature_' + Math.random().toString(36).substring(2, 15),
        });
      }, 100);
    });
  }